{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fa9c33b8-d816-4fa8-9520-971e87f02289",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.610998Z",
     "iopub.status.busy": "2022-04-15T02:09:04.610746Z",
     "iopub.status.idle": "2022-04-15T02:09:04.911852Z",
     "shell.execute_reply": "2022-04-15T02:09:04.911413Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.610931Z"
    }
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "096b198d-0e2a-42d0-af2f-5958486a76bb",
   "metadata": {},
   "source": [
    "## 小结\n",
    "1. Series:  一维标记的数组型对象\n",
    "    1. 能够保存任何类型的数据\n",
    "    2. 包含了标签和数据\n",
    "2. 创建方法\n",
    "    1. 通过list创建\n",
    "    2. 通过ndarray创建\n",
    "    3. 通过dict创建\n",
    "    4. 获取数据和索引的方法\n",
    "3. 基本用法\n",
    "    1. 通过索引获取/切片数据\n",
    "    2. name属性\n",
    "    3. 对应关系不被运算结果影响\n",
    "    4. head函数和tail函数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "05857af0-6bf0-4785-85cb-af4f4798eaf9",
   "metadata": {},
   "source": [
    "## Series创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b0a79fab-47f6-4848-91a7-a905f54a054b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.913684Z",
     "iopub.status.busy": "2022-04-15T02:09:04.913529Z",
     "iopub.status.idle": "2022-04-15T02:09:04.923139Z",
     "shell.execute_reply": "2022-04-15T02:09:04.922104Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.913667Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "4    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1. 通过list创建Series\n",
    "s1 = pd.Series([1, 2, 3, 4, 5])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a6c8a405-0015-4820-b212-ad45870cb52a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.924468Z",
     "iopub.status.busy": "2022-04-15T02:09:04.924259Z",
     "iopub.status.idle": "2022-04-15T02:09:04.928920Z",
     "shell.execute_reply": "2022-04-15T02:09:04.928181Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.924438Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看类型\n",
    "type(s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4d6737df-2f4a-487e-8e94-98683e4609d3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.930178Z",
     "iopub.status.busy": "2022-04-15T02:09:04.929799Z",
     "iopub.status.idle": "2022-04-15T02:09:04.935178Z",
     "shell.execute_reply": "2022-04-15T02:09:04.934621Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.930153Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "0    1\n",
      "1    2\n",
      "2    3\n",
      "3    4\n",
      "4    5\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "# 2. 通过NumPy数组创建Series\n",
    "import numpy as np\n",
    "\n",
    "arr = np.arange(1, 6)\n",
    "print(arr)\n",
    "s2 = pd.Series(arr)\n",
    "print(s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "10920e8e-a505-4c81-a897-d54052123e97",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.936469Z",
     "iopub.status.busy": "2022-04-15T02:09:04.936080Z",
     "iopub.status.idle": "2022-04-15T02:09:04.943189Z",
     "shell.execute_reply": "2022-04-15T02:09:04.942259Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.936445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 手动指定索引\n",
    "s2 = pd.Series(arr, index=[\"a\", \"b\", \"c\", \"d\", \"e\"])\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "32be4728-a0df-4f78-b0fe-b9a1064851eb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:43:47.541468Z",
     "iopub.status.busy": "2022-04-15T02:43:47.540858Z",
     "iopub.status.idle": "2022-04-15T02:43:48.490291Z",
     "shell.execute_reply": "2022-04-15T02:43:48.489191Z",
     "shell.execute_reply.started": "2022-04-15T02:43:47.541439Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Length of values (5) does not match length of index (4)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [16]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 手动指定索引\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m s2 \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSeries\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mc\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43md\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      3\u001b[0m s2\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/series.py:442\u001b[0m, in \u001b[0;36mSeries.__init__\u001b[0;34m(self, data, index, dtype, name, copy, fastpath)\u001b[0m\n\u001b[1;32m    440\u001b[0m     index \u001b[38;5;241m=\u001b[39m default_index(\u001b[38;5;28mlen\u001b[39m(data))\n\u001b[1;32m    441\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m is_list_like(data):\n\u001b[0;32m--> 442\u001b[0m     \u001b[43mcom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequire_length_match\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    444\u001b[0m \u001b[38;5;66;03m# create/copy the manager\u001b[39;00m\n\u001b[1;32m    445\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, (SingleBlockManager, SingleArrayManager)):\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/common.py:557\u001b[0m, in \u001b[0;36mrequire_length_match\u001b[0;34m(data, index)\u001b[0m\n\u001b[1;32m    553\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    554\u001b[0m \u001b[38;5;124;03mCheck the length of data matches the length of the index.\u001b[39;00m\n\u001b[1;32m    555\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    556\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(index):\n\u001b[0;32m--> 557\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    558\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLength of values \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    559\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(data)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m) \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    560\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdoes not match length of index \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    561\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(index)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    562\u001b[0m     )\n",
      "\u001b[0;31mValueError\u001b[0m: Length of values (5) does not match length of index (4)"
     ]
    }
   ],
   "source": [
    "# 手动指定索引\n",
    "s2 = pd.Series(arr, index=[\"a\", \"b\", \"c\", \"d\"])\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "94c3a31a-31a8-4f7e-8bc3-0676b1f1a601",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:47:36.907469Z",
     "iopub.status.busy": "2022-04-15T02:47:36.907216Z",
     "iopub.status.idle": "2022-04-15T02:47:36.957617Z",
     "shell.execute_reply": "2022-04-15T02:47:36.956718Z",
     "shell.execute_reply.started": "2022-04-15T02:47:36.907443Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Length of values (5) does not match length of index (6)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Input \u001b[0;32mIn [17]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 手动指定索引\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m s2 \u001b[38;5;241m=\u001b[39m \u001b[43mpd\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mSeries\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43ma\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mb\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mc\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43md\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43me\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      3\u001b[0m s2\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/series.py:442\u001b[0m, in \u001b[0;36mSeries.__init__\u001b[0;34m(self, data, index, dtype, name, copy, fastpath)\u001b[0m\n\u001b[1;32m    440\u001b[0m     index \u001b[38;5;241m=\u001b[39m default_index(\u001b[38;5;28mlen\u001b[39m(data))\n\u001b[1;32m    441\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m is_list_like(data):\n\u001b[0;32m--> 442\u001b[0m     \u001b[43mcom\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrequire_length_match\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdata\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindex\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    444\u001b[0m \u001b[38;5;66;03m# create/copy the manager\u001b[39;00m\n\u001b[1;32m    445\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(data, (SingleBlockManager, SingleArrayManager)):\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/common.py:557\u001b[0m, in \u001b[0;36mrequire_length_match\u001b[0;34m(data, index)\u001b[0m\n\u001b[1;32m    553\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    554\u001b[0m \u001b[38;5;124;03mCheck the length of data matches the length of the index.\u001b[39;00m\n\u001b[1;32m    555\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    556\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m!=\u001b[39m \u001b[38;5;28mlen\u001b[39m(index):\n\u001b[0;32m--> 557\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\n\u001b[1;32m    558\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mLength of values \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    559\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(data)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m) \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    560\u001b[0m         \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdoes not match length of index \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    561\u001b[0m         \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m(\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mlen\u001b[39m(index)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m)\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m    562\u001b[0m     )\n",
      "\u001b[0;31mValueError\u001b[0m: Length of values (5) does not match length of index (6)"
     ]
    }
   ],
   "source": [
    "# 手动指定索引\n",
    "s2 = pd.Series(arr, index=[\"a\", \"b\", \"c\", \"d\", \"e\", \"f\"])\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "746f9337-bd7d-425c-ba57-b17871410108",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:17:18.315495Z",
     "iopub.status.busy": "2022-04-15T02:17:18.315103Z",
     "iopub.status.idle": "2022-04-15T02:17:18.320623Z",
     "shell.execute_reply": "2022-04-15T02:17:18.319840Z",
     "shell.execute_reply.started": "2022-04-15T02:17:18.315469Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小熊', 'age': 7, 'class': '2105班'}\n"
     ]
    }
   ],
   "source": [
    "# 3. 通过dict来创建Series\n",
    "dict1 = {\"name\": \"小熊\", \"age\": 7, \"class\": \"2105班\"}\n",
    "print(dict1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2dd8376a-8f64-4a1a-91df-8b50d25d2029",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:17:20.676117Z",
     "iopub.status.busy": "2022-04-15T02:17:20.675871Z",
     "iopub.status.idle": "2022-04-15T02:17:20.681619Z",
     "shell.execute_reply": "2022-04-15T02:17:20.681042Z",
     "shell.execute_reply.started": "2022-04-15T02:17:20.676091Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name        小熊\n",
       "age          7\n",
       "class    2105班\n",
       "dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建Series\n",
    "s3 = pd.Series(dict1)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "29b622dc-dcb5-4189-832b-542d7c7d7c27",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:19:41.999782Z",
     "iopub.status.busy": "2022-04-15T02:19:41.999521Z",
     "iopub.status.idle": "2022-04-15T02:19:42.007230Z",
     "shell.execute_reply": "2022-04-15T02:19:42.006454Z",
     "shell.execute_reply.started": "2022-04-15T02:19:41.999757Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name        小熊\n",
       "age          7\n",
       "class    2105班\n",
       "dtype: object"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 通过dict来创建Series\n",
    "dict1 = {\"name\": \"小熊\", \"age\": 7, \"class\": \"2105班\"}\n",
    "s3 = pd.Series(dict1, index=[\"name\", \"age\", \"class\"])\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0e9a0fd9-e2c9-4f98-9206-8ae60a8103a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:50:52.400058Z",
     "iopub.status.busy": "2022-04-15T02:50:52.399800Z",
     "iopub.status.idle": "2022-04-15T02:50:52.408893Z",
     "shell.execute_reply": "2022-04-15T02:50:52.408075Z",
     "shell.execute_reply.started": "2022-04-15T02:50:52.400032Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name    小熊\n",
       "age      7\n",
       "dtype: object"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 通过dict来创建Series\n",
    "dict1 = {\"name\": \"小熊\", \"age\": 7, \"class\": \"2105班\"}\n",
    "s3 = pd.Series(dict1, index=[\"name\", \"age\"])\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9e3790e0-5ee6-4800-9aed-f4529eb22317",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:51:23.072566Z",
     "iopub.status.busy": "2022-04-15T02:51:23.072315Z",
     "iopub.status.idle": "2022-04-15T02:51:23.080989Z",
     "shell.execute_reply": "2022-04-15T02:51:23.079882Z",
     "shell.execute_reply.started": "2022-04-15T02:51:23.072540Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name        小熊\n",
       "age          7\n",
       "class    2105班\n",
       "sex        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 通过dict来创建Series\n",
    "dict1 = {\"name\": \"小熊\", \"age\": 7, \"class\": \"2105班\"}\n",
    "s3 = pd.Series(dict1, index=[\"name\", \"age\", \"class\", \"sex\"])\n",
    "s3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4a52eb5-ccde-421a-87d6-84464f4a3889",
   "metadata": {},
   "source": [
    "## 查看索引和值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8e81d2bb-7bee-4499-90b4-d8d96f67a1dd",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.945071Z",
     "iopub.status.busy": "2022-04-15T02:09:04.944694Z",
     "iopub.status.idle": "2022-04-15T02:09:04.950575Z",
     "shell.execute_reply": "2022-04-15T02:09:04.950001Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.945044Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看Series的值\n",
    "s1.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "5e489e14-a894-43dd-9572-a3d116b61b50",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:09:04.953547Z",
     "iopub.status.busy": "2022-04-15T02:09:04.953325Z",
     "iopub.status.idle": "2022-04-15T02:09:04.959708Z",
     "shell.execute_reply": "2022-04-15T02:09:04.958853Z",
     "shell.execute_reply.started": "2022-04-15T02:09:04.953525Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RangeIndex(start=0, stop=5, step=1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看Series的索引\n",
    "s1.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a95893e3-90d8-4923-a3ed-092b1544ba83",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:28:59.634136Z",
     "iopub.status.busy": "2022-04-15T02:28:59.633879Z",
     "iopub.status.idle": "2022-04-15T02:28:59.639853Z",
     "shell.execute_reply": "2022-04-15T02:28:59.639164Z",
     "shell.execute_reply.started": "2022-04-15T02:28:59.634110Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['小熊', 7, '2105班'], dtype=object)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看Series的值\n",
    "s3.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "53c1450c-25e7-4b0c-b4c6-253f3ba7af6a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T02:29:11.946638Z",
     "iopub.status.busy": "2022-04-15T02:29:11.946352Z",
     "iopub.status.idle": "2022-04-15T02:29:11.952236Z",
     "shell.execute_reply": "2022-04-15T02:29:11.951426Z",
     "shell.execute_reply.started": "2022-04-15T02:29:11.946606Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['name', 'age', 'class'], dtype='object')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看Series的索引\n",
    "s3.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccb917cc-77b4-484a-ba18-55c83023c4b2",
   "metadata": {},
   "source": [
    "## Series的基本用法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "40a19fae-06b1-462f-9a51-60faf21be624",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T03:23:46.249708Z",
     "iopub.status.busy": "2022-04-15T03:23:46.249448Z",
     "iopub.status.idle": "2022-04-15T03:23:46.255963Z",
     "shell.execute_reply": "2022-04-15T03:23:46.255187Z",
     "shell.execute_reply.started": "2022-04-15T03:23:46.249681Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name        小熊\n",
       "age          7\n",
       "class    2105班\n",
       "sex        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "1cd8e11c-60a1-4768-abaf-4a6d99afe934",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T03:27:57.276301Z",
     "iopub.status.busy": "2022-04-15T03:27:57.276061Z",
     "iopub.status.idle": "2022-04-15T03:27:57.282516Z",
     "shell.execute_reply": "2022-04-15T03:27:57.281810Z",
     "shell.execute_reply.started": "2022-04-15T03:27:57.276263Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name     False\n",
       "age      False\n",
       "class    False\n",
       "sex       True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.1 识别s3中的空值\n",
    "s3.isnull()  # 空元素位置为True，非空元素位置为False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "877656ab-6ea1-4c3c-8b37-ba3592c5b36c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T03:38:27.840696Z",
     "iopub.status.busy": "2022-04-15T03:38:27.840451Z",
     "iopub.status.idle": "2022-04-15T03:38:27.846526Z",
     "shell.execute_reply": "2022-04-15T03:38:27.845808Z",
     "shell.execute_reply.started": "2022-04-15T03:38:27.840671Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name      True\n",
       "age       True\n",
       "class     True\n",
       "sex      False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.2 识别s3中的非空值\n",
    "s3.notnull()  # 空元素位置为False，非空元素位置为True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "deda9d83-c264-44ab-b1ea-70f7d8ab7010",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T08:09:10.411979Z",
     "iopub.status.busy": "2022-04-15T08:09:10.411680Z",
     "iopub.status.idle": "2022-04-15T08:09:10.416918Z",
     "shell.execute_reply": "2022-04-15T08:09:10.416237Z",
     "shell.execute_reply.started": "2022-04-15T08:09:10.411911Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2. 通过索引获取数据\n",
    "# 2.1 通过下标取数据\n",
    "s3[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "dc904fce-d86e-4b12-ac14-cbc8d97354ed",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T11:26:32.916160Z",
     "iopub.status.busy": "2022-04-15T11:26:32.915681Z",
     "iopub.status.idle": "2022-04-15T11:26:32.931676Z",
     "shell.execute_reply": "2022-04-15T11:26:32.930973Z",
     "shell.execute_reply.started": "2022-04-15T11:26:32.916101Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.2 通过索引的标签名取数据\n",
    "s3[\"age\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "ca166df5-cd79-4068-b608-007eb1160444",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T11:32:04.046693Z",
     "iopub.status.busy": "2022-04-15T11:32:04.046406Z",
     "iopub.status.idle": "2022-04-15T11:32:04.064848Z",
     "shell.execute_reply": "2022-04-15T11:32:04.063980Z",
     "shell.execute_reply.started": "2022-04-15T11:32:04.046668Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age      7\n",
       "sex    NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.3 通过下标取多个元素数据\n",
    "s3[[1, 3]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "4165b0f2-c4a6-4451-92e9-5a11e14dde81",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T11:35:37.782104Z",
     "iopub.status.busy": "2022-04-15T11:35:37.781811Z",
     "iopub.status.idle": "2022-04-15T11:35:37.791089Z",
     "shell.execute_reply": "2022-04-15T11:35:37.790170Z",
     "shell.execute_reply.started": "2022-04-15T11:35:37.782072Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age      7\n",
       "sex    NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.4 通过索引的标签名取多个元素数据\n",
    "s3[[\"age\", \"sex\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f8626852-b0c5-46b4-8af1-b81bd9f49bfe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T11:41:37.541878Z",
     "iopub.status.busy": "2022-04-15T11:41:37.541633Z",
     "iopub.status.idle": "2022-04-15T11:41:37.548777Z",
     "shell.execute_reply": "2022-04-15T11:41:37.548092Z",
     "shell.execute_reply.started": "2022-04-15T11:41:37.541853Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age          7\n",
       "class    2105班\n",
       "dtype: object"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.5 通过索引下标切片\n",
    "s3[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "c6cb1c6b-5aeb-47b1-a2c3-32e3278164cf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T11:43:19.671044Z",
     "iopub.status.busy": "2022-04-15T11:43:19.670795Z",
     "iopub.status.idle": "2022-04-15T11:43:19.684529Z",
     "shell.execute_reply": "2022-04-15T11:43:19.683924Z",
     "shell.execute_reply.started": "2022-04-15T11:43:19.671018Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "age          7\n",
       "class    2105班\n",
       "sex        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.6 通过索引标签名切片\n",
    "s3[\"age\":\"sex\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e331a393-d840-48a9-a1e4-c97aa47a75fb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:13:21.867490Z",
     "iopub.status.busy": "2022-04-15T12:13:21.867180Z",
     "iopub.status.idle": "2022-04-15T12:13:21.874407Z",
     "shell.execute_reply": "2022-04-15T12:13:21.873711Z",
     "shell.execute_reply.started": "2022-04-15T12:13:21.867463Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    1\n",
      "b    2\n",
      "c    3\n",
      "d    4\n",
      "e    5\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.7 使用布尔索引取值\n",
    "print(s2)\n",
    "s2[s2 > 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e44b1c93-4a12-4f81-8301-c51064038e84",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:22:53.819020Z",
     "iopub.status.busy": "2022-04-15T12:22:53.818739Z",
     "iopub.status.idle": "2022-04-15T12:22:53.823557Z",
     "shell.execute_reply": "2022-04-15T12:22:53.822968Z",
     "shell.execute_reply.started": "2022-04-15T12:22:53.818970Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a     2\n",
      "b     4\n",
      "c     6\n",
      "d     8\n",
      "e    10\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "print(s2 * 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "55249855-d629-4e1a-b6a3-496ff47db6cf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:25:26.896559Z",
     "iopub.status.busy": "2022-04-15T12:25:26.896315Z",
     "iopub.status.idle": "2022-04-15T12:25:26.902352Z",
     "shell.execute_reply": "2022-04-15T12:25:26.901724Z",
     "shell.execute_reply.started": "2022-04-15T12:25:26.896536Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a     2\n",
       "b     4\n",
       "c     6\n",
       "d     8\n",
       "e    10\n",
       "dtype: int64"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3 索引与数据的对应关系不受运算结果影响\n",
    "s2 * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "9abb6385-3999-42f1-adde-a46724c1e2b8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:25:42.975517Z",
     "iopub.status.busy": "2022-04-15T12:25:42.975269Z",
     "iopub.status.idle": "2022-04-15T12:25:42.981583Z",
     "shell.execute_reply": "2022-04-15T12:25:42.980833Z",
     "shell.execute_reply.started": "2022-04-15T12:25:42.975492Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    False\n",
       "b    False\n",
       "c    False\n",
       "d     True\n",
       "e     True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 > 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "5236b2df-d54f-4554-b747-6c30356a5179",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:36:14.419229Z",
     "iopub.status.busy": "2022-04-15T12:36:14.418979Z",
     "iopub.status.idle": "2022-04-15T12:36:14.424880Z",
     "shell.execute_reply": "2022-04-15T12:36:14.423922Z",
     "shell.execute_reply.started": "2022-04-15T12:36:14.419203Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "day\n",
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "Name: test, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4 name属性\n",
    "s2.name = \"test\"\n",
    "s2.index.name = \"day\"\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "33b72823-d3fd-4580-834d-f2712394a20a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T12:50:56.206879Z",
     "iopub.status.busy": "2022-04-15T12:50:56.206590Z",
     "iopub.status.idle": "2022-04-15T12:50:56.213267Z",
     "shell.execute_reply": "2022-04-15T12:50:56.212499Z",
     "shell.execute_reply.started": "2022-04-15T12:50:56.206829Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "day\n",
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "Name: test, dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.1 head函数，查看前几行数据\n",
    "s2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "33dbd5fd-231c-428e-91f1-cad2049b8715",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T13:02:13.306045Z",
     "iopub.status.busy": "2022-04-15T13:02:13.305603Z",
     "iopub.status.idle": "2022-04-15T13:02:13.311513Z",
     "shell.execute_reply": "2022-04-15T13:02:13.310902Z",
     "shell.execute_reply.started": "2022-04-15T13:02:13.306019Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "day\n",
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "Name: test, dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "3f2f3aaf-216d-4405-8af6-40326feed287",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T13:10:55.786862Z",
     "iopub.status.busy": "2022-04-15T13:10:55.786486Z",
     "iopub.status.idle": "2022-04-15T13:10:55.792924Z",
     "shell.execute_reply": "2022-04-15T13:10:55.792278Z",
     "shell.execute_reply.started": "2022-04-15T13:10:55.786841Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "day\n",
       "a    1\n",
       "b    2\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "Name: test, dtype: int64"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.2 tail函数，查看后几行数据\n",
    "s2.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ce901116-636d-4731-acbd-0b52edca7c95",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-04-15T13:13:12.519173Z",
     "iopub.status.busy": "2022-04-15T13:13:12.518812Z",
     "iopub.status.idle": "2022-04-15T13:13:12.524909Z",
     "shell.execute_reply": "2022-04-15T13:13:12.524254Z",
     "shell.execute_reply.started": "2022-04-15T13:13:12.519144Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "day\n",
       "c    3\n",
       "d    4\n",
       "e    5\n",
       "Name: test, dtype: int64"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2.tail(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "baf6e264-70f5-4011-9e6a-552267efc2a6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
